/**
 * Project Name:九色鹿
 * File Name:RestAuthenticationEntryPoint.java
 * Package Name:com.fxp.project.jiuselu.security.component
 * Date:2021年03月14日 14:43
 * Copyright (c) 2021, Jason.Wang All Rights Reserved.
 */
package com.fxp.project.jiuselu.security.component;

import com.alibaba.fastjson.JSONObject;
import com.fxp.project.jiuselu.commons.code.RetCode;
import com.fxp.project.jiuselu.commons.constant.ConstantCode;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * ClassName:RestAuthenticationEntryPoint<br/>
 * Function: RestAuthenticationEntryPoint<br/>
 * Date:2021年03月14日 14:43<br/>
 *
 * @author Jason.Wa ng
 * @version 1.0.0.0
 * @see
 * @since JDK1.8
 */
@Component
public class RestAuthenticationEntryPoint implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) throws IOException {
        RetCode retCode = new RetCode(ConstantCode.USER_NOT_LOGGED_IN.getCode(),ConstantCode.USER_NOT_LOGGED_IN.getMessage());
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json");
        response.setStatus(HttpStatus.UNAUTHORIZED.value());
        response.getWriter().println(JSONObject.toJSONString(retCode));
        response.getWriter().flush();
    }
}
